﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CjcPhotoPrint
{
	/// <summary>
	/// Interaction logic for Preview.xaml
	/// </summary>
	public partial class Preview : UserControl
	{
		private CropAdorner cropAdorner;
		private Point? dragOrigin;

		public static DependencyProperty SourceProperty = DependencyProperty.Register(
			"Source",
			typeof( TransformationContext ),
			typeof( Preview ) );

		public Preview()
		{
			InitializeComponent();
		}

		public TransformationContext Source
		{
			get { return (TransformationContext)GetValue( SourceProperty ); }
			set { SetValue( SourceProperty, value ); }
		}

		private void cropSelect_Loaded( object sender, RoutedEventArgs e )
		{
			var element = sender as FrameworkElement;
			var layer = AdornerLayer.GetAdornerLayer( element );
			layer.Add( cropAdorner = new CropAdorner( element ) );
		}

		private void Rectangle_MouseDown( object sender, MouseButtonEventArgs e )
		{
			dragOrigin = e.GetPosition( sender as IInputElement );
		}

		private void Rectangle_MouseMove( object sender, MouseEventArgs e )
		{
			if ( dragOrigin.HasValue )
			{
				var pos = e.GetPosition( sender as IInputElement );
				var offset = new Vector( pos.X - dragOrigin.Value.X, pos.Y - dragOrigin.Value.Y );
				dragOrigin = pos;

				cropAdorner.AdjustBounds( offset.X, offset.Y, offset.X, offset.Y );
			}
		}

		private void Rectangle_MouseUp( object sender, MouseButtonEventArgs e )
		{
			dragOrigin = null;
		}

		private void Rectangle_MouseLeave( object sender, MouseEventArgs e )
		{
			dragOrigin = null;
		}
	}
}